/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBAnggota;
import database.DBBarang;
import database.DBPegawai;
import database.DBPengadaan;
import database.DBPenjualan;
import database.DBProduksi;
import database.DBSupplier;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author elfatah
 */
public class ViewServlet extends HttpServlet {

    DBAnggota anggotaDb = new DBAnggota();
    DBPegawai pegawaiDb = new DBPegawai();
    DBPengadaan pengadaanDb = new DBPengadaan();
    DBProduksi produksiDb = new DBProduksi();
    DBPenjualan penjualanDb = new DBPenjualan();
    DBSupplier suppliyerDb = new DBSupplier();
    DBBarang barangDb = new DBBarang();
    private String action, userPath;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        try {
//            /* TODO output your page here. You may use following sample code. */
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet ViewServlet</title>");  
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet ViewServlet at " + request.getContextPath () + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
//        } finally { 
//            out.close();
//        }
//    } 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        //action = request.getParameter("action");
        action = request.getServletPath();

        //  processRequest(request, response);
        //====View semua ini=====
        if (session.getAttribute("user") == (null)) {
            String url = "/WEB-INF/view/login.jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/view_pegawai")) {
            try {
                List listPegawai = pegawaiDb.getPegawaiDetails();
                request.setAttribute("listPegawai", listPegawai);

                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);

            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else if (action.equals("/view_anggota")) {
            try {
                List listAnggota = anggotaDb.getAnggotaDetails();
                request.setAttribute("listAnggota", listAnggota);
//               
                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);

            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/view_produksi")) {
            try {
                List listProduksi = produksiDb.getProduksiDetails();
                request.setAttribute("listProduksi", listProduksi);
//               
                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/view_penjualan")) {
            try {
                List listPenjualan = penjualanDb.getPenjualanDetails();
                request.setAttribute("listPenjualan", listPenjualan);
                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/view_pengadaan")) {
            try {
                List listPengadaan = pengadaanDb.getPengadaanDetails();
                request.setAttribute("listPengadaan", listPengadaan);

                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/view_laporan_produksi")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/view_laporan_penjualan")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } //=====insert semua ini=======
        else if (action.equals("/insert_pengadaan")) {
            try {
                List listPegawaiPengadaan = pegawaiDb.getPegawaiDetails();
                request.setAttribute("listPegawaiPengadaan", listPegawaiPengadaan);
                List listSuppliyerPengadaan = suppliyerDb.getSupplierDetails();
                request.setAttribute("listSuppliyerPengadaan", listSuppliyerPengadaan);

                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/insert_penjualan")) {
            try {
                List listPegawaiPenjualan = pegawaiDb.getPegawaiDetails();
                request.setAttribute("listPegawaiPenjualan", listPegawaiPenjualan);
//                List listBarangPenjualan = barangDb.getBarangDetails();
//                request.setAttribute("listBarangPenjualan", listBarangPenjualan);

                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (action.equals("/insert_anggota")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/insert_pegawai")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/insert_produksi")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/password")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/view_laporan")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/dashboard")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/login")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/logout")) {

            String url = "/WEB-INF/view/" + action + ".jsp";
            request.getRequestDispatcher(url).forward(request, response);
        } else if (action.equals("/view_laporan_kas")) {
            try {
                List listGaji = pegawaiDb.getPegawaiDetails();
                request.setAttribute("listGaji", listGaji);
                String url = "/WEB-INF/view/" + action + ".jsp";
                request.getRequestDispatcher(url).forward(request, response);
            } //String url = "/WEB-INF/view/" + action + ".jsp";
            //        try {
            //            request.getRequestDispatcher(url).forward(request, response);
            //        } catch (ServletException ex) {
            //            ex.printStackTrace();
            //        } catch (IOException ex) {
            //            ex.printStackTrace();
            //        }
            catch (SQLException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(ViewServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
